﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YesSql;
using OrchardCore.DisplayManagement.Notify;

using Ruitu.Edu.TrainingDemo.Models;
using Microsoft.Extensions.Localization;
using Microsoft.AspNetCore.Mvc.Localization;

namespace Ruitu.Edu.TrainingDemo.Controllers
{
    public class DatabaseStorageController : Controller
    {
        private readonly INotifier _notifier;
        private readonly ISession _session;
        private readonly IHtmlLocalizer<DatabaseStorageController> H;

        public DatabaseStorageController(
            ISession session,
            INotifier notifier,
            IHtmlLocalizer<DatabaseStorageController> stringLocalizer)
        {
            _session = session;
            _notifier = notifier;
            H = stringLocalizer;
        }

        /// 访问路径：/Ruitu.Edu.TrainingDemo/DatabaseStorage/CreateBooks
        public IActionResult CreateBooks()
        {
            return View();
        }

        [HttpPost, 
         ActionName(nameof(CreateBooks)),
         /*ValidateAntiForgeryToken*/ /* OrchardCore 默认已经启用，所以这里就不需要了*/
        ]
        public async Task<IActionResult> CreateBooksPost()
        {
            foreach (var book in CreateDemoBooks())
            {
                await _session.SaveAsync(book);
            }
            
            await _notifier.InformationAsync(H["Books created successfully!"]);

            return RedirectToAction(nameof(CreateBooks));
        }

        private static List<Book> CreateDemoBooks()
        {
            return new List<Book>
            {
                new Book
                {
                    Title = "C# 9.0 in a Nutshell",
                    CoverPhotoUrl = "/Cover.jpg",
                    Author = "Joseph Albahari and Ben Albahari",
                    Description = "A comprehensive guide to C# 9.0, covering the latest features and best practices."
                },
                new Book
                {
                    Title = "ASP.NET Core in Action",
                    CoverPhotoUrl = "/Cover.jpg",
                    Author = "Andrew Lock",
                    Description = "Learn to build web applications with ASP.NET Core, including MVC, Web API, and SignalR."
                },
                new Book
                {
                    Title = "Entity Framework Core in Action",
                    CoverPhotoUrl = "/Cover.jpg",
                    Author = "Jon P Smith",
                    Description = "Master Entity Framework Core with practical examples and real-world scenarios."
                },
                new Book
                {
                    Title = "Design Patterns in C#",
                    CoverPhotoUrl = "/Cover.jpg",
                    Author = "Vaskaran Sarcar",
                    Description = "Learn and implement design patterns in C# with practical examples and use cases."
                },
                new Book
                {
                    Title = "Clean Code in C#",
                    CoverPhotoUrl = "/Cover.jpg",
                    Author = "Jason Alls",
                    Description = "Write clean, maintainable, and efficient C# code following best practices and principles."
                }
            };
        }
    }
}
